package com.raymond.cmpp.client.cmpp2.decoding;

import com.raymond.cmpp.common.codec.annotation.MsgCodec;
import com.raymond.cmpp.common.codec.decoding.BaseDecoder;
import com.raymond.cmpp.common.constance.GlobalConstance;
import com.raymond.cmpp.common.enums.CommandIdType;
import com.raymond.cmpp.common.msg.BaseAgreement;
import com.raymond.cmpp.common.msg.request.DeliverRequestMessage;
import com.raymond.cmpp.common.msg.request.ReportRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

import java.util.List;

/**
 * 状态报告和上行解码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-10 10:48
 */
@MsgCodec(commandId = CommandIdType.DELIVER_REQUEST)
public class DeliverRequestMessageDecoder extends BaseDecoder {
    @Override
    public void decode(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) throws Exception {
        DeliverRequestMessage requestMessage = new DeliverRequestMessage(msg.getSequenceId());
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        requestMessage.setMsgId(bodyBuffer.readLong());
        requestMessage.setDestId(bodyBuffer.readCharSequence(21, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        requestMessage.setServiceId(bodyBuffer.readCharSequence(10, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        requestMessage.setTpPid(bodyBuffer.readUnsignedByte());
        requestMessage.setTpUdhi(bodyBuffer.readUnsignedByte());
        requestMessage.setMsgFmt(bodyBuffer.readUnsignedByte());

        requestMessage.setSrcTerminalId(bodyBuffer.readCharSequence(21, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());

        // requestMessage.setSrcterminalType(bodyBuffer.readUnsignedByte());//CMPP2.0
        // SrcterminalType不进行编解码
        short registeredDelivery =bodyBuffer.readUnsignedByte();
        short frameLength = (short)(bodyBuffer.readUnsignedByte() & 0xffff);

        if (registeredDelivery == 0) {
            byte[] contentBytes = new byte[frameLength];
            bodyBuffer.readBytes(contentBytes);
            requestMessage.setMsgContentBytes(contentBytes);
            requestMessage.setMsgLength(frameLength);
        } else {
            ReportRequestMessage reportRequestMessage = new ReportRequestMessage(msg.getSequenceId());
            reportRequestMessage.setMsgId(bodyBuffer.readLong());
            reportRequestMessage.setStat(bodyBuffer.readCharSequence(7, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            reportRequestMessage.setSubmitTime(
                    bodyBuffer.readCharSequence(10, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            reportRequestMessage.setDoneTime(
                    bodyBuffer.readCharSequence(10, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            reportRequestMessage.setDestTerminalId(
                    bodyBuffer.readCharSequence(21, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            reportRequestMessage.setSmscSequence(bodyBuffer.readUnsignedInt());
            requestMessage.setReportRequestMessage(reportRequestMessage);
        }

        //剩下的字节全部读取
        requestMessage.setReserved(bodyBuffer.readCharSequence(8, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        ReferenceCountUtil.release(bodyBuffer);
        out.add(requestMessage);


    }
}
